package com.bakar.util;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.TimeZone;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.google.appengine.api.users.User;

public class BakarUtils {
	
	private static String emailMessage =  getEmail();

	public static String getLoginId(User user) {
		String loginId = null;
		try {
			loginId = user.getEmail()
					.substring(0, user.getEmail().indexOf("@"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		return loginId;
	}

	public static String getLoginId(String userEmail) {
		String loginId = null;
		try {
			if (userEmail.indexOf("@") != -1) {
				loginId = userEmail.substring(0, userEmail.indexOf("@"));
			} else {
				loginId = userEmail;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return loginId;
	}

	public static String convertToSmallCase(String value) {
		return (value != null ? value.toLowerCase() : null);
	}

	// "May 31, 2011 23:00:00"
	public static String convertDateToString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss");
		sdf.setTimeZone(TimeZone.getTimeZone("IST"));
		return sdf.format(date);

	}

	public static void main(String... args) {
		System.out.println(emailMessage);
	}

	public static void sendmail(String emailAddress, String localUserName, String subject, String customMessage) {
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);
		String message = customMessage + emailMessage.replace("$USERNAME$", localUserName);
		try {
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress("yogesh4982@gmail.com",
					"Bakar Admin"));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
					emailAddress, emailAddress));
			msg.setSubject(subject);
			msg.setText(message);
			Transport.send(msg);
		} catch (AddressException e) { 
			e.printStackTrace();
		} catch (MessagingException e) { 
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static String getEmail(){
		StringBuffer sb = new StringBuffer();
		sb.append("\t\n\n");
		sb.append("\t\n\n");
		sb.append("\t You can access Bakar through the following link:");
		sb.append("\t\n\n");
		sb.append("\t\t http://bakaronly.appspot.com");
		sb.append("\t\n\n");
		sb.append("\tYou need to perform following steps to start.");
		sb.append("\t\n\n");
		sb.append("\t\t1.	Login to Bakar using your gmail id");
		sb.append("\t\n");
		sb.append("\t\t2.	After successful login select  'Invite friend' link from the 'Update user' menu");
		sb.append("\t\n");
		sb.append("\t\t3.	Select username radio button");
		sb.append("\t\n");
		sb.append("\t\t4.	Add  ");
		sb.append("\t$USERNAME$");
		sb.append("\t in the text box");
		sb.append("\t\n");
		sb.append("\t\t5.	Click Invite friend button. ");
		

		return sb.toString();
	}

}
